﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_vote : BoardSubPartBase
{
	private BoardThread CurrentThread;
	private Vote CurrentVote;

	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];
		this.CurrentVote = (Vote)this.Page.Items["CurrentVote"];
	}

	protected void Page_Load(Object sender, EventArgs e)
	{
		Vote_Bind();
	}

	protected void Vote_Bind()
	{
		if (!this.CurrentThread.IsBlocked && this.CurrentVote != null)
		{
			if (!Page.IsPostBack)
			{
				VoteQuestionLiteral.Text = this.CurrentVote.Question;
				VotePeriodLiteral.Text = String.Format(GetLocalResourceObject("Vote_PeriodFormat").ToString(), this.CurrentVote.BeginDate, this.CurrentVote.EndDate);
				VoteIsOpenLiteral.Visible = this.CurrentVote.IsOpen;
			}

			Boolean isVotePeriod = Page.User.Identity.IsAuthenticated && this.CurrentVote.IsVotePeriod;

			if (isVotePeriod && !this.CurrentVote.ExistsVoter(base.CurrentUserInfo.ProviderUserKey))
				VoteForm_Bind();
			else
				VoteResult_Bind();
		}
		else
		{
			this.Visible = false;
		}
	}

	protected void VoteResult_Bind()
	{
		VoteResult.Visible = true;
		VoteForm.Visible = !VoteResult.Visible;

		VoteAsnwerRepeater.DataSource = this.CurrentVote.AnswerCollection;
		VoteAsnwerRepeater.DataBind();

		TotalVoteCountLiteral.Text = String.Format(GetLocalResourceObject("Vote_TotalCountFormat").ToString(), this.CurrentVote.TotalVoterCount, this.CurrentVote.TotalVoteCount);

		VoterRepeater.DataSource = this.CurrentVote.AllVoterCollection;
		VoterRepeater.DataBind();
	}

	protected void VoteForm_Bind()
	{
		VoteResult.Visible = false;
		VoteForm.Visible = !VoteResult.Visible;

		ListControl answerListControl = null;

		if (this.CurrentVote.IsMultiSelection)
			answerListControl = new CheckBoxList();
		else
			answerListControl = new RadioButtonList();

		answerListControl.DataSource = this.CurrentVote.AnswerCollection;
		answerListControl.DataTextField = "Answer";
		answerListControl.DataValueField = "AnswerID";
		answerListControl.DataBind();

		AnswerPlaceHolder.Controls.Add(answerListControl);

		ViewResultButton.OnClientClick = String.Format("document.getElementById('{0}').style.display = 'none';", VoteForm.ClientID);
		DoVoteButton.OnClientClick = String.Format("document.getElementById('{0}').style.display = 'none';", VoteForm.ClientID);
	}

	protected void ViewResultButton_Click(Object sender, EventArgs e)
	{
		VoteResult_Bind();
	}

	protected void DoVoteButton_Click(Object sender, EventArgs e)
	{
		VoteDoVoteResult result;

		ListControl answerListControl = (ListControl)AnswerPlaceHolder.Controls[0];

		if (answerListControl.SelectedIndex < 0)
		{
			ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
				VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteUnknownAnswer"), true), true);
			return;
		}

		if (this.CurrentVote.IsMultiSelection)
		{
			String[] answerIDs = new String[this.CurrentVote.AnswerCollection.Count];

			ListItemCollection answerItemCollection = answerListControl.Items;

			for (Int32 i = 0; i < this.CurrentVote.AnswerCollection.Count; i++)
			{
				if (answerItemCollection[i].Selected)
					answerIDs[i] = answerItemCollection[i].Value;
			}

			result = this.CurrentVote.DoVote(answerIDs, base.CurrentUserInfo.ProviderUserKey, base.CurrentUserInfo.Name);
		}
		else
		{
			result = this.CurrentVote.DoVote(((ListControl)AnswerPlaceHolder.Controls[0]).SelectedValue, base.CurrentUserInfo.ProviderUserKey, base.CurrentUserInfo.Name);
		}

		switch (result)
		{
			case VoteDoVoteResult.Success:
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteSuccess"), true), true);
				this.VoteResult_Bind();
				break;
			case VoteDoVoteResult.Already:
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteAlready"), true), true);
				break;
			case VoteDoVoteResult.UnknownAnswer:
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteUnknownAnswer"), true), true);
				break;
			case VoteDoVoteResult.NoPeriod:
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteNoPeriod"), true), true);
				break;
			default:
				ScriptManager.RegisterStartupScript(Page, this.GetType(), "VoteResult",
					VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Vote_Do_VoteFailure"), true), true);
				break;
		}
	}

	protected void VoteAsnwerRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		{
			Repeater answerRepeater = (Repeater)e.Item.FindControl("AnswerVoterRepeater");
			answerRepeater.DataSource = ((VoteAnswer)e.Item.DataItem).VoterCollection;
			answerRepeater.DataBind();
		}
	}
}
